<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

use mbilling\BillingBundle\Model\MessagesModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Model\UsersModel;
use mbilling\BillingBundle\Entity\messages;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\MessagesForm;

class MessagesController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $message_model = new MessagesModel($em);
        $user_model = new UsersModel($em);
        
        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $users_string = array();
        if ($userCurrent->isAdmin())
        {
            if ($this->get('session')->get('user'))
                $messages = $message_model->findByUserToId($this->get('session')->get('user')->getId());
            else
                $messages = $message_model->findAll();
        }
        else
            $messages = $message_model->findByUserToId($userCurrent->getId());
        foreach ($messages as $message)
        {
            $user_from = $user_model->findByPK($message->getUserFrom());
            $user_to = $user_model->findByPK($message->getUserTo());
            $users_string[$user_from->getId()] = $user_from->getLogin();
            $users_string[$user_to->getId()] = $user_to->getLogin();
        }

        rsort($messages);
        if ($userCurrent->isAdmin())
        {
            if ($this->get('session')->get('user'))
                return $this->render('BillingBundle:Messages:user_index.html.twig', array('messages' => $messages, 'users' => $users_string, 'count' => count($messages)));
            else
                return $this->render('BillingBundle:Messages:admin_index.html.twig', array('messages' => $messages, 'users' => $users_string, 'count' => count($messages)));
        }
        else
            return $this->render('BillingBundle:Messages:user_index.html.twig', array('messages' => $messages, 'users' => $users_string, 'count' => count($messages)));        
    }
    public function show_specAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $message_model = new MessagesModel($em);
        $user_model = new UsersModel($em);

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $users_string = array();
        $message = $message_model->findByPK($id);
        if ($userCurrent->isAdmin() && $this->get('session')->get('user'))
        {
            if ((!$message->getWasRead()) && ($message->getUserTo() == $this->get('session')->get('user')->getId()))
            {
                $message->setWasRead('1');
                $message_model->create($message);
            }
        }
        elseif ((!$message->getWasRead()) && ($message->getUserTo() == $userCurrent->getId()))
        {
            $message->setWasRead('1');
            $message_model->create($message);
        }

        $user_from = $user_model->findByPK($message->getUserFrom());
        $user_to = $user_model->findByPK($message->getUserTo());
        $users_string[$user_from->getId()] = $user_from->getLogin();
        $users_string[$user_to->getId()] = $user_to->getLogin();

        if ($userCurrent->isAdmin())
        {
            if ($this->get('session')->get('user'))
                return $this->render('BillingBundle:Messages:user_spec.html.twig', array('message' => $message, 'users' => $users_string));
            else
                return $this->render('BillingBundle:Messages:admin_spec.html.twig', array('message' => $message, 'users' => $users_string));
        }
        else
            return $this->render('BillingBundle:Messages:user_spec.html.twig', array('message' => $message, 'users' => $users_string));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $message_model = new MessagesModel($em);
        $user_model = new UsersModel($em);
        $message = new messages();
        //$message_model->create($message->initDefaultEntity());
        //return new RedirectResponse($this->generateUrl('messages_show'));

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();
        $users = $user_model->findAll();
        $users_string = array();
        foreach($users as $user)
        {
            if ($user->getId() != $userCurrent->getId())
            {
                $users_string[$user->getId()] = $user->getLogin();
            }
        }
        $form = $this->createForm(new MessagesForm($users_string), $message);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $message->setUserFrom($userCurrent->getId());
                $message->setWasRead('0');
                $message_model->create($form->getData());

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('14');//страница уведомления
                $log->setObjectId($message->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание уведомления
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('messages_show'));
            }
        }
        return $this->render('BillingBundle:Messages:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $message_model = new MessagesModel($em);
        $message_model->removeByPK($id);

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('14');//страница уведомления
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление уведомления
        $log_model->create($log);
        return new RedirectResponse($this->generateUrl('messages_show'));
    }
    public function renderCountAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();
        $message_model = new MessagesModel($em);
        if ($userCurrent == 'anon.')
        {
            return new Response('');
        }
        if ($userCurrent->isAdmin() && $this->get('session')->get('user'))
            $messages = $message_model->findByWasNotRead($this->get('session')->get('user')->getId());
        else
            $messages = $message_model->findByWasNotRead($userCurrent->getId());
        return new Response(count($messages));
    }
}